//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.qy137.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qy137.entity.ZoneCourse;
import com.qy137.vo.CourseVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.qy137.dao.ZoneCourseDao;
import org.qy137.feign.CoreFeign;
import org.qy137.service.ZoneCourseService;
import org.qy137.utils.CommonResult;
import org.qy137.utils.ShujuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ZoneCourseServiceImpl implements ZoneCourseService {
    @Autowired
    private ZoneCourseDao zoneCourseDao;
    @Autowired
    private CoreFeign coreFeign;

    public ZoneCourseServiceImpl() {
    }

    public CommonResult getAllDetail(Integer page, Integer limit, CourseVo courseVo) {
        PageHelper.startPage(page, limit);
        List<ShujuUtil> allZoneCourse = this.zoneCourseDao.getAllZoneCourse(courseVo.getId(), courseVo.getCourseTitle(), courseVo.getIsDisable());
        PageInfo<ShujuUtil> info = new PageInfo(allZoneCourse);
        return info.getList().size() > 0 ? new CommonResult(2000, "查询成功", info) : new CommonResult(5000, "查询失败", (Object)null);
    }

    public CommonResult deleteZoneCourse(String id) {
        ZoneCourse zoneCourse = new ZoneCourse();
        zoneCourse.setId(id);
        zoneCourse.setIsDeleted(1);
        int i = this.zoneCourseDao.updateById(zoneCourse);
        return i > 0 ? new CommonResult(2000, "删除成功", (Object)null) : new CommonResult(5000, "删除失败", (Object)null);
    }

    public CommonResult getAllCourse(Integer page, Integer limit, CourseVo courseVoz) {
        Object result = this.coreFeign.findCourseByPage(page, limit, courseVoz, (String)null).getResult();
        ObjectMapper objectMapper = new ObjectMapper();
        Page page1 = (Page)objectMapper.convertValue(result, Page.class);
        List records = page1.getRecords();
        ArrayList<CourseVo> courseVos = new ArrayList();

        CourseVo courseVo;
        for(Iterator var9 = records.iterator(); var9.hasNext(); courseVos.add(courseVo)) {
            Object record = var9.next();
            courseVo = (CourseVo)objectMapper.convertValue(record, CourseVo.class);
            QueryWrapper<ZoneCourse> wrapper = new QueryWrapper();
            wrapper.eq("zone_id", courseVoz.getZoneId());
            wrapper.eq("course_id", courseVo);
            wrapper.eq("is_deleted", 0);
            List<ZoneCourse> zoneCourses = this.zoneCourseDao.selectList(wrapper);
            if (zoneCourses.size() > 0) {
                courseVo.setAdd(1);
            } else {
                courseVo.setAdd(0);
            }
        }

        page1.setRecords(courseVos);
        return new CommonResult(2000, "查询成功", page1);
    }

    public CommonResult addZoneCourse(String zoneId, String courseId) {
        ZoneCourse zoneCourse = new ZoneCourse();
        zoneCourse.setGmtCreate(LocalDateTime.now());
        zoneCourse.setGmtModified(LocalDateTime.now());
        zoneCourse.setIsDeleted(0);
        zoneCourse.setIsDisable(0);
        zoneCourse.setZoneId(zoneId);
        zoneCourse.setCourseId(courseId);
        int insert = this.zoneCourseDao.insert(zoneCourse);
        return insert > 0 ? new CommonResult(2000, "添加成功", (Object)null) : new CommonResult(5000, "添加失败", (Object)null);
    }
}
